package com.turbo.boot.crawler.spider.worker;

import com.turbo.boot.crawler.dao.dal.CrawlerConfigDal;
import com.turbo.boot.crawler.dao.entity.CrawlerConfigEntity;
import com.turbo.boot.crawler.dao.entity.CrawlerResultEntity;
import com.xuxueli.crawler.parser.PageParser;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 描述
 *
 * @author wuyn
 * @since 2024/4/21
 */
public abstract class BaseTurboCrawlerPageParser<PageVo> extends PageParser<PageVo> {

    @Resource
    protected CrawlerConfigDal crawlerConfigDal;

    protected CrawlerConfigEntity getCrawlerConfig() {

        if (null == crawlerConfigDal) {
            return null;
        }

        return crawlerConfigDal.selectByCrawlerParserRef(this.getClass().getAnnotation(Service.class).value());
    }


    protected CrawlerResultEntity buildCrawlerResultEntity(){
        CrawlerConfigEntity configEntity = getCrawlerConfig();
        CrawlerResultEntity crawlerResult = new CrawlerResultEntity();
        crawlerResult.setGroupId(configEntity.getGroupId());
        crawlerResult.setSourceUrl(configEntity.getTargetUrls());

        return crawlerResult;
    }


    public String name() {
        return this.getClass().getAnnotation(Service.class).value();
    }

    public void parse(Document html, Element pageVoElement, PageVo pageVo) {

    }

    public void parse(PageVo pageVo) {

    }
}
